package org.sample.commons.edoc;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PdfUtils {

    private static BaseFont textFont;

    static {
        try {
            textFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void initialFont(BaseFont font) {
        textFont = font;
    }

    public static void render(String templateURL, OutputStream output, Map<String, Object> parameters) throws IOException, DocumentException {
        render(new PdfReader(templateURL), output, parameters);
    }

    public static void render(InputStream template, OutputStream output, Map<String, Object> parameters) throws IOException, DocumentException {
        render(new PdfReader(template), output, parameters);
    }

    public static void render(PdfReader reader, OutputStream output, Map<String, Object> parameters) throws IOException, DocumentException {
        PdfStamper stamper = new PdfStamper(reader, output);

        try {
            float textSize = 11f;

            AcroFields form = stamper.getAcroFields();

            Set<Map.Entry<String, Object>> entries = parameters.entrySet();
            for (Map.Entry<String, Object> entry : entries) {

                String key = entry.getKey();
                Object value = entry.getValue();
                if (value != null)
                    if (value instanceof Image) {

                        List<AcroFields.FieldPosition> items = form.getFieldPositions(key);
                        if (items != null)
                            for (AcroFields.FieldPosition fieldPosition : items) {
                                Rectangle rectangle = fieldPosition.position;
                                int pageNum = fieldPosition.page;

                                Image image = (Image) value;
                                image.scaleAbsolute(rectangle.getWidth(), rectangle.getHeight());
                                image.setAbsolutePosition(rectangle.getLeft(), rectangle.getBottom());

                                stamper.getOverContent(pageNum).addImage(image);
                            }
                    } else {
                        form.setFieldProperty(key, "textfont", textFont, null);
                        form.setFieldProperty(key, "textsize", textSize, null);
                        form.setField(key, String.valueOf(value));
                    }
            }

            stamper.setFormFlattening(true);
            stamper.close();
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}